package com.sx.gongzhong.widget;

import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.sx.gongzhong.R;
import com.sx.gongzhong.adapter.ReportTypeAdapter;
import com.sx.gongzhong.model.PublicBean;

import java.util.List;

/**
 * Created by HuGei on 2017/6/30.
 * 自定义举报/禁言的dialog
 */
public class ReportDialog extends Dialog {

    public ReportDialog(Context context, int theme) {
        super(context, theme);
    }

    public ReportDialog(Context context) {
        super(context);
    }
    private  static  TypeCallback mCallback;
    private static int current=0;

    /**
     * 定义一个接口
     */
    public interface TypeCallback {
        void backType(PublicBean publicBean);
    }


    /***
     *   设置成false back键不能退出
     * @param flag
     */
    @Override
    public void setCancelable(boolean flag) {
        super.setCancelable(flag);
    }

    /**
     * Helper class for creating a custom dialog
     */
    public static class Builder {
        private Context context;
        private List<PublicBean> publicBeanList; //内容
        private ListView listView;
        private String title; // 对话框标题

        public Builder(Context context) {
            this.context = context;
        }

        /**
         * 使用资源设置对话框标题信息
         * @param title
         * @return
         */
        public Builder setTitle(int title) {
            this.title = (String) context.getText(title);
            return this;
        }

        /**
         * 使用字符串设置对话框标题信息
         * @param title
         * @return
         */
        public Builder setTitle(String title) {
            this.title = title;
            return this;
        }

        /*接口回调*/
        public Builder loadType(TypeCallback callback) {
            mCallback = callback;
            return this;
        }
        /**
         * 设置内容数据
         */
        public Builder setDataList(List<PublicBean> publicBeanList) {
            this.publicBeanList = publicBeanList;
            return this;
        }


        /**
         * 创建自定义的对话框
         */
        public ReportDialog create() {
            LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            // LayoutInflater inflater=LayoutInflater.from(context);
            // 实例化自定义的对话框主题
            final ReportDialog dialog = new ReportDialog(context, R.style.Dialog);

            View layout = inflater.inflate(R.layout.report_dialog, null);
            dialog.addContentView(layout,
                    new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
                            LinearLayout.LayoutParams.WRAP_CONTENT));
            // 设置对话框标题
            if (title!=null){
                ((TextView) layout.findViewById(R.id.dialog_title)).setText(title);
            }else {
                (layout.findViewById(R.id.dialog_title)).setVisibility(View.GONE);
            }

            // 设置对话框内容
            if (publicBeanList != null) {
                listView = (ListView)layout.findViewById(R.id.listView);
                final ReportTypeAdapter adapter=new ReportTypeAdapter(context,R.layout.item_select_text,publicBeanList);
                listView.setAdapter(adapter);
                listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        adapter.setSelect(position);
                        current=position;
                    }
                });
            }

            // 设置返回按钮事件
            Button bckButton = ((Button) layout.findViewById(R.id.dialog_back));

            bckButton.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    dialog.dismiss();
                }
            });

            // 设置确定按钮事件
            Button cfmButton = ((Button) layout.findViewById(R.id.dialog_confirm));
            cfmButton.setOnClickListener(new View.OnClickListener() {
                    public void onClick(View v) {
                      if (mCallback!=null&&publicBeanList!=null&&publicBeanList.size()>0){
                          mCallback.backType(publicBeanList.get(current));
                      }
                        dialog.dismiss();
                    }
            });
            dialog.setContentView(layout);
            //点击空白和back键有效
            dialog.setCancelable(true);
            return dialog;
        }
    }
}
